package org.megha.blog.example.files;

import java.io.File;

/**
 * A program to list all files and directories in a given directory.
 *
 * @author megha birmiwal
 */
public class DirectoryLister {

	public void listDirectories(String filepath) {

		File topDir = new File(filepath);
		if (!topDir.exists()) {
			System.out.println("Directory does not exist: " + filepath);
			return;
		}

		if (!topDir.isDirectory()) {
			System.out.println("Not a directory: " + filepath);
			return;
		}

		File[] files = topDir.listFiles();

		for (File file : files) {
			if (file.isHidden()) {
				// do not show hidden files
				continue;
			}
			if (file.isDirectory()) {
				System.out.println(file.getName() + " - D");
			} else {
				System.out.println(file.getName() + " - " + file.length());
			}
// You can use the File APIs to play around with files, eg. rename or delete etc.
//			String name = file.getName();
//			name = name.replace(".png", ".jpg");
//			name = name + ".txt";
//		    file.renameTo(new File(name));
//          file.delete();
		}
	}

	public static void main(String[] args) {
		DirectoryLister lister = new DirectoryLister();
		lister.listDirectories("/");
	}
}
